package cn.iocoder.yudao.module.alipay.service.alipayorder;

import cn.iocoder.yudao.module.alipay.controller.app.alipayorder.vo.AlipayOrderCreateReqVO;
import cn.iocoder.yudao.module.alipay.controller.app.alipayorder.vo.AlipayOrderExportReqVO;
import cn.iocoder.yudao.module.alipay.controller.app.alipayorder.vo.AlipayOrderPageReqVO;
import cn.iocoder.yudao.module.alipay.controller.app.alipayorder.vo.AlipayOrderUpdateReqVO;
import cn.iocoder.yudao.module.alipay.dal.dataobject.infoauthaliapp.InfoAuthAliAppDO;
import cn.iocoder.yudao.module.alipay.dal.dataobject.payrecord.PayRecordDO;
import cn.iocoder.yudao.module.alipay.dal.mysql.infoauthaliapp.InfoAuthAliAppMapper;
import cn.iocoder.yudao.module.alipay.dal.mysql.payrecord.PayRecordMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import java.time.LocalDateTime;
import java.util.*;

import cn.iocoder.yudao.module.alipay.dal.dataobject.elong.alipayorder.AlipayOrderDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.alipay.convert.alipayorder.AlipayOrderConvert;
import cn.iocoder.yudao.module.alipay.dal.mysql.alipayorder.AlipayOrderMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.alipay.enums.ErrorCodeConstants.*;

/**
 * 支付宝订单 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
@Slf4j
public class AlipayOrderServiceImpl implements AlipayOrderService {

    @Resource
    private AlipayOrderMapper orderMapper;

    @Resource
    private InfoAuthAliAppMapper infoAuthAliAppMapper;

    @Resource
    private PayRecordMapper payRecordMapper;

    @Override
    public Long createOrder(AlipayOrderCreateReqVO createReqVO) {
        // 插入
        AlipayOrderDO order = AlipayOrderConvert.INSTANCE.convert(createReqVO);
        InfoAuthAliAppDO infoAuthAliAppDO = infoAuthAliAppMapper.selectByAppId(createReqVO.getAppId());
        order.setCreator(infoAuthAliAppDO.getCreator());
        orderMapper.insert(order);
        // 返回
        return order.getId();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateOrder(AlipayOrderUpdateReqVO updateReqVO) {
        // 校验存在
        validateOrderExists(updateReqVO.getId());
        // 更新
        AlipayOrderDO updateObj = AlipayOrderConvert.INSTANCE.convert(updateReqVO);
        log.info("-------------------updateObj.getOrderStatues():"+updateObj.getOrderStatues());
        //支付成功則在支付记录表中生成记录
        if(updateObj.getOrderStatues().equals("1")){
            PayRecordDO payRecordDO = new PayRecordDO();
            payRecordDO.setOrderId(updateObj.getId());
            payRecordDO.setPayTime(LocalDateTime.now());
            payRecordDO.setRecordType("input");
            payRecordDO.setOutTradeNo(updateObj.getOutTradeNo());
            payRecordDO.setTradeNo(updateObj.getTradeNo());
            payRecordDO.setTotalAmount(updateObj.getTotalAmount());
            payRecordDO.setBuyerId(updateObj.getBuyerId());
            payRecordDO.setAppId(updateObj.getAppId());
            payRecordDO.setCreator(updateObj.getCreator());
            payRecordMapper.insert(payRecordDO);
        }
        orderMapper.updateById(updateObj);
    }

    @Override
    public void deleteOrder(Long id) {
        // 校验存在
        validateOrderExists(id);
        // 删除
        orderMapper.deleteById(id);
    }

    private void validateOrderExists(Long id) {
        if (orderMapper.selectById(id) == null) {
            throw exception(ORDER_NOT_EXISTS);
        }
    }

    @Override
    public AlipayOrderDO getOrder(Long id) {
        return orderMapper.selectById(id);
    }

    @Override
    public List<AlipayOrderDO> getOrderList(Collection<Long> ids) {
        return orderMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<AlipayOrderDO> getOrderPage(AlipayOrderPageReqVO pageReqVO) {
        return orderMapper.selectPage(pageReqVO);
    }

    @Override
    public List<AlipayOrderDO> getOrderList(AlipayOrderExportReqVO exportReqVO) {
        return orderMapper.selectList(exportReqVO);
    }

}
